﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	//player move
	public float moveSpeed;
	private CharacterController characterController;

	//joint
	public Rigidbody head;
	public Rigidbody body;
	public float headForce;

	//ray cast
	public LayerMask layerMask;
	private Vector3 currentLookTarget;

	//animator
	public Animator bodyAnimator;

	//hit
	public float[] hitForce;
	public float timeBetweenHits = 2.5f;
	private bool isHit = false;
	private float timeSinceHit = 0;
	private int hitNumber = -1;
	private bool isDead;

	//particle
	private DeathParticles deathParticle;

	// Use this for initialization
	void Start () {
		characterController = GetComponent<CharacterController> () as CharacterController;
		deathParticle = GetComponentInChildren<DeathParticles> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (isDead) return; 

		Vector3 moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
		characterController.SimpleMove (moveDirection * moveSpeed);

		if (isHit) {
			timeSinceHit += Time.deltaTime;
			if (timeSinceHit > timeBetweenHits) {
				isHit = false;
				timeSinceHit = 0;
			}
		}
	}

	void FixedUpdate() {
		Vector3 moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));

		if (moveDirection == Vector3.zero) {
			//
			bodyAnimator.SetBool ("IsMoving", false);
		} else {
			head.AddForce (transform.right * headForce, ForceMode.Force);
			bodyAnimator.SetBool ("IsMoving", true);
		}

		UpdatePlayerDirection ();
	}

	//update player rotate
	void UpdatePlayerDirection() {
		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		Debug.DrawRay (ray.origin, ray.direction * 1000, Color.green);

		if (Physics.Raycast (ray, out hit, 1000, layerMask, QueryTriggerInteraction.Ignore)) {
			if (hit.point != currentLookTarget) {
				currentLookTarget = hit.point;

				//rotate
				Vector3 targetPosition = new Vector3 (currentLookTarget.x, transform.position.y, currentLookTarget.z);
				Quaternion rotate = Quaternion.LookRotation (targetPosition - transform.position);
				transform.rotation = Quaternion.Lerp (transform.rotation, rotate, Time.deltaTime * 10.0f);
			}
		}
	}

	//
	void OnTriggerEnter(Collider other) {
		Alien alien = other.gameObject.GetComponent<Alien> ();
		if (alien) {
			if (!isHit) {
				hitNumber += 1;
				CameraShake shake = Camera.main.GetComponent<CameraShake> ();
				if (hitNumber < hitForce.Length) {
					//
					shake.intensity = hitForce [hitNumber];
					shake.Shake ();
				} else {
					//death
					Die ();
				}

				isHit = true;
				SoundManager.Instance.PlayOneShot (SoundManager.Instance.hurt);
			}

			alien.Die ();
		}
	}

	private void Die() {
		isDead = true;
		bodyAnimator.SetBool ("IsMoving", false);

		body.transform.parent = null;
		body.isKinematic = false;
		body.useGravity = true;
		body.gameObject.GetComponent<CapsuleCollider> ().enabled = true;
		body.gameObject.GetComponent<Gun> ().enabled = false;

		Destroy (head.gameObject.GetComponent<HingeJoint> ());
		head.transform.parent = null;
		head.isKinematic = false;
		head.useGravity = true;

		deathParticle.Active ();

		SoundManager.Instance.PlayOneShot (SoundManager.Instance.marineDeath);
		Destroy (gameObject);
	}
}
